function formatNumber (n) {
  const str = n.toString()
  return str[1] ? str : `0${str}`
}
export function formatTime (date) {
  const year = date.getFullYear()
  const month = date.getMonth() + 1
  const day = date.getDate()

  const hour = date.getHours()
  const minute = date.getMinutes()
  // const second = date.getSeconds()

  const t1 = [year, month, day].map(formatNumber).join('-')
  const t2 = [hour, minute].map(formatNumber).join(':')

  return `${t1} ${t2} `
}
export function formatTimeIncludeText (date, fmt) {
  //将时间戳格式化为xxxx年xx月xx日
  date = new Date(parseInt(date))
  const year = date.getFullYear()
  const month = date.getMonth() + 1
  const day = date.getDate()

  return formatNumber(year)+'年'+ formatNumber(month)+ '月'+ formatNumber(day) +'日'
}
export function getUserIP(onNewIP) { //  onNewIp - your listener function for new IPs
  //compatibility for firefox and chrome
  var myPeerConnection = window.RTCPeerConnection || window.mozRTCPeerConnection || window.webkitRTCPeerConnection;
  var pc = new myPeerConnection({
      iceServers: []
    }),
    noop = function() {},
    localIPs = {},
    ipRegex = /([0-9]{1,3}(\.[0-9]{1,3}){3}|[a-f0-9]{1,4}(:[a-f0-9]{1,4}){7})/g,
    key;

  function iterateIP(ip) {
    if (!localIPs[ip]) onNewIP(ip);
    localIPs[ip] = true;
  }

  //create a bogus data channel
  pc.createDataChannel("");

  // create offer and set local description
  pc.createOffer().then(function(sdp) {
    sdp.sdp.split('\n').forEach(function(line) {
      if (line.indexOf('candidate') < 0) return;
      line.match(ipRegex).forEach(iterateIP);
    });

    pc.setLocalDescription(sdp, noop, noop);
  }).catch(function(reason) {
    // An error occurred, so handle the failure to connect
  });

  //sten for candidate events
  pc.onicecandidate = function(ice) {
    if (!ice || !ice.candidate || !ice.candidate.candidate || !ice.candidate.candidate.match(ipRegex)) return;
    ice.candidate.candidate.match(ipRegex).forEach(iterateIP);
  };
}

export function dec2hex (dec, len) {
  var hex = "";
  while( dec ) {
    var last = dec & 15;
    hex = String.fromCharCode(((last>9)?55:48)+last) + hex;
    dec >>= 4;
  }
  if(len) {
    while(hex.length < len) hex = '0' + hex;
  }
  return hex;
};
export function toFixedBit(num, totalBit, isFront, fixedChar, alwaysCut) {
  if (totalBit === void 0) { totalBit = 10; }
  if (isFront === void 0) { isFront = true; }
  if (fixedChar === void 0) { fixedChar = "0"; }
  if (alwaysCut === void 0) { alwaysCut = false; }
  var nn = num.toString();
  if (!alwaysCut && nn.length >= totalBit) {
    return nn;
  }
  var rtn = "";
  for (var i = 0; i < totalBit; i++) {
    rtn += fixedChar;
  }
  if (isFront) {
    rtn += nn;
    rtn = rtn.substr(rtn.length - totalBit);
  }
  else {
    rtn = nn + rtn;
    rtn = rtn.substr(0, totalBit);
  }
  return rtn;
};
export function strToHexCharCode(str,len) {
  var val="";
  var strCode =''
  for(var i = 0; i < str.length; i++){
    if(val == "")
      val = str.charCodeAt(i).toString(16);
    else
      val +=  str.charCodeAt(i).toString(16);
  }
  return val
}

export function  clearInfo() {

}
export function  getNewDate(str){
  if(!str){
    return 0;
  }
  var arr=str.split(" ");
  var d=arr[0].split("-");
  var t=arr[1].split(":");
  var date = new Date();
  date.setUTCFullYear(d[0], d[1] - 1, d[2]);
  date.setUTCHours(t[0], t[1], 0);
  return date;
}
export function  formatDateTime(timeStamp) {
  if(!timeStamp){
      return ''
  }
  else{
    var date = new Date();
    date.setTime(timeStamp * 1000);
    var y = date.getFullYear();
    var m = date.getMonth() + 1;
    m = m < 10 ? ('0' + m) : m;
    var d = date.getDate();
    d = d < 10 ? ('0' + d) : d;
    return y + '-' + m + '-' + d
  }
};
export function  formatDateTimeSecond(timeStamp) {
  if(!timeStamp){
    return ''
  }
  else{
    var date = new Date();
    date.setTime(timeStamp * 1000);
    var y = date.getFullYear();
    var m = date.getMonth() + 1;
    m = m < 10 ? ('0' + m) : m;
    var d = date.getDate();
    d = d < 10 ? ('0' + d) : d;
    var h=date.getHours();
    h=h < 10 ? ('0' + h) : h;
    var Min =date.getMinutes();
    Min=Min < 10 ? ('0' + Min) : Min;
    return y + '-' + m + '-' + d+'-'+h+':'+Min
  }
};

export function  formatgetTime(timeStamp) {
    var date =new Date(timeStamp);
    var y = date.getFullYear();
    var m = date.getMonth() + 1;
    m = m < 10 ? ('0' + m) : m;
    var d = date.getDate();
    d = d < 10 ? ('0' + d) : d;
    return y + '-' + m + '-' + d
};
export function  formatDateYear(timeStamp) {
  var date = new Date();
  date.setTime(timeStamp * 1000);
  var y = date.getFullYear();
  var m = date.getMonth() + 1;
  m = m < 10 ? ('0' + m) : m;
  var d = date.getDate();
  d = d < 10 ? ('0' + d) : d;

  return y
};
